package chap04.PriorityQ;

/**
 * Created by root on 2017/7/14.
 */
public class PriorityQ {
    private int maxSize;
    private long[] queArray;
    private int nItems;

    public PriorityQ(int s){
        maxSize = s;
        queArray = new long[maxSize];
        nItems = 0;
    }

    public void insert(long item){
        if (nItems == 0) {
            queArray[nItems++] = item;
        } else {
            int i = 0;
            while (item < queArray[i]) {
                if (i == nItems) {
                    break;
                }
                i++;
            }
            for (int j=nItems-1; j>=i; j--) {
                queArray[j+1] = queArray[j];
            }
            queArray[i] = item;
            nItems++;
        }
    }

    public void insertion(long item){
        if (nItems == 0) {
            queArray[nItems++] = item;
        } else {
            int i = nItems - 1;
            while (item > queArray[i] && i>=0) {
                queArray[i+1] = queArray[i];
                i--;
            }
            queArray[i+1] = item;
            nItems++;
        }
    }

    public long remove() {
        return queArray[--nItems];
    }

    public long peekMin() {
        return queArray[nItems-1];
    }

    public boolean isEmpty(){
        return nItems==0;
    }

    public boolean isFull(){
        return nItems == maxSize;
    }
}
